<template>
  <div>
    <div class="cart-content">
      <div class="cart__total" @click="toggleList">
        <div class="cart__icon">
          <van-icon name="logistics"></van-icon>
        </div>
        <div class="cart__price">￥{{totalPrice.toFixed(2)}}</div>
      </div>
      <div class="cart__btn" v-bind:class="{enough: isEnough}" @click="subOrder">
        {{subText}}
      </div>
    </div>
    <transition name="fold">
      <div class="cart-list" v-show="listShow">
        <div class="haeder">
          <div class="tit">购物清单</div>
          <div class="clear" @click="empty">清空</div>
        </div>
        <div class="content">
          <ul>
            <li class="item" v-for="food in products">
              <div class="tit">{{food.name}}</div>
              <div class="control">
                <van-stepper v-model="food.count" :min="0" integer />
              </div>
              <div class="price">￥{{food.price*food.count}}</div>
            </li>
          </ul>
        </div>
      </div>
    </transition>
    <transition class="fade">
      <div class="mask" @click="hideList" v-show="listShow"></div>
    </transition>
  </div>
</template>


<script>
  export default {
    components: {},
    props: {
      selectProducts: {
        type: Array
      },
      limitPrice: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        listShow: false,
      }
    },
    computed: {
      //总价
      totalPrice() {
        let total = 0;
        this.selectProducts.forEach(element => {
          total += element.count * element.price;
        });
        return total;
      },
      //判断大于配送金额
      isEnough() {
        if (this.totalPrice >= this.limitPrice) {
          return true;
        }
        return false;
      },
      subText() {
        if (this.totalPrice >= this.limitPrice) {
          return '提交订单';
        } else {
          return '差' + (this.limitPrice - this.totalPrice) + '元起送';
        }
      },
      products() {
        let products = [];
        this.selectProducts.forEach(el => {
          if (el.count) {
            products.push(el);
          }
        });
        return products;
      }
    },
    methods: {
      //提交工单
      subOrder() {
        if (this.selectProducts.length == 0) {
          this.$toast('您未选择任何商品');
        } else {
          this.$emit('submit', this.selectProducts);
        }
      },
      toggleList() {
        if (!this.totalPrice) {
          this.$toast('客官您未添加任何商品哦~');
          return;
        }
        this.listShow = !this.listShow;
      },
      //隐藏购物车列表
      hideList() {
        this.listShow = false;
      },
      //清空购物车
      empty() {
        this.listShow = false;
        this.selectProducts.forEach(el => {
          el.count = 0;
        });
      }
    }
  }

</script>

<style lang="less" scoped>
  @import '../styles/variable.less';

  .cart-content {
    position: relative;
    z-index: 9;
    // display: flex;
    height: 38px;
    line-height: 38px;

    .cart__total {
      display: flex;
      width: calc(100% - 120px);
      background: #ffffff;

      .cart__icon {
        position: relative;
        top: -8px;
        margin: 0 10px;
        width: 40px;
        flex: 0 0 40px;
        height: 40px;
        line-height: 40px;
        background-color: @col-the;
        text-align: center;
        border-radius: 50%;

        i {
          position: relative;
          top: 4px;
          font-size: 22px;
          color: #ffffff;
        }
      }

      .cart__price {
        color: @col-the;
        font-size: 20px;
        font-weight: bold;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }

    .cart__btn {
      position: absolute;
      right: 0;
      top: 0;
      width: 120px;
      text-align: center;
      color: #ffffff;
      background-color: #a9a6a6;

      &.enough {
        background-color: #4cd964;
      }
    }
  }

  .cart-list {
    position: absolute;
    z-index: 1;
    bottom: 38px;
    transform: translateY(0);
    width: 100%;
    background: #fff;

    .haeder {
      height: 40px;
      line-height: 40px;
      padding: 0 18px;
      background: #f3f5f7;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      font-size: 14px;

      .tit {
        float: left;
        color: #333;
      }

      .clear {
        color: #00a0dc;
        float: right;
      }
    }

    .content {
      list-style: none;
      color: #666;
      padding: 0 10px 10px 10px;
      overflow: auto;

      .item {
        position: relative;
        padding: 10px 0;
        clear: both;
        overflow: hidden;

        .tit {
          position: relative;
          top: 4px;
          float: left;
          font-size: 14px;
        }

        .price {
          margin-right: 10px;
          margin-top: 3px;
          float: right;
          color: #ff0000;
        }

        .control {
          float: right;
        }
      }

      li:not(:last-child) {
        border-bottom: 1px solid #eeeeee;
      }
    }
  }

  .mask {
    position: fixed;
    z-index: 0;
    left: 0;
    top: 0;
    bottom: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.3);
  }

  .fold-enter,
  .fold-leave-active {
    transform: translateY(100%);
  }

  .fold-enter-active,
  .fold-leave-active {
    transition: 0.4s all;
  }

  .fade-enter,
  .fade-leave-active {
    opacity: 0;
    background: rgba(7, 17, 27, 0);
  }

  .fade-enter-active,
  .fade-leave-active {
    transition: 0.5s all;
  }

</style>
